package com.r2coding.common.util;

import org.apache.commons.lang.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author:98k灬
 * @since:2022/08/05 13:00
 **/
public final class CommonUtil {
    /**
     * 检查手机号是否合法
     *
     * @param phone 待检查的手机号
     * @return 是否合法
     */
    public static boolean checkPhone(String phone) {
        String pattern = "0?(13|14|15|17|18|19)[0-9]{9}";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(phone);
        return m.matches();
    }

    /**
     * 检查用户名是否合法
     *
     * @param name 用户名
     * @return 是否合法
     */
    public static boolean checkUserName(String name) {
        String pattern = "[A-Za-z0-9_\\-\\u4e00-\\u9fa5]+";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(name);
        return m.matches();
    }

    /**
     * 用户手机号码的打码隐藏加星号加*
     *
     * @return 处理完成的身份证
     */
    public static String phoneMask(String phone) {
        String res = "";
        if (!StringUtils.isEmpty(phone) && phone.length() == 11) {
            StringBuilder stringBuilder = new StringBuilder(phone);
            res = stringBuilder.replace(3, 7, "****").toString();
        }
        return res;
    }

    private CommonUtil () {}
}
